package cn.com.bluemoon.daps.standard.excel.easypoi;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import cn.com.bluemoon.daps.standard.entity.DapStandardBasicDic;
import cn.com.bluemoon.daps.standard.excel.BaseCacheVerifyHandler;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.io.Closeable;
import java.util.List;
import java.util.Map;

/**
 * @author Jarod.Kong
 * @date 2020/8/26 17:26
 */
@Slf4j
public class BsAndDicVerifyHandler extends BaseCacheVerifyHandler
        implements IExcelVerifyHandler<DapStandardBasicDic>, Closeable {
    //有序的中文名称
    private final List<String> hadReadStandardNames = Lists.newArrayList();
    private final List<String> hadReadStandardCodes = Lists.newArrayList();
    // 已发布中的代码英文名称 +代码中文名称  DapStandardBasicDic::getBaseCode, DapStandardBasicDic::getBaseCodeName
    private Map<String, String> dbStandardDics = Maps.newHashMap();

    public BsAndDicVerifyHandler() {

    }

    /**
     * 导入校验方法
     *
     * @param obj 当前对象
     * @return
     */
    @Override
    public ExcelVerifyHandlerResult verifyHandler(DapStandardBasicDic obj) {
        int currentRowIndex = sheetRowIndex.incrementAndGet();
        if (obj != null) {
            String errorMsg = null;
            List<String> errorMsgs = Lists.newArrayList();

            // 针对标准中文名称校验
            String codeNameCheckKey = obj.getCodeName() + obj.getBaseCode();
            int rowIndex = hadReadStandardNames.indexOf(codeNameCheckKey);
            hadReadStandardNames.add(codeNameCheckKey);
            if (rowIndex >= 0) {
                errorMsg = String.format("第%d行与前第%d行的[代码值名称]重复",
                        currentRowIndex, rowIndex + 2);
                errorMsgs.add(errorMsg);
            }
            // 针对getBaseCode校验
            String codeValueCheckKey = obj.getCodeValue() + obj.getBaseCode();
            int rowIndex1 = hadReadStandardCodes.indexOf(codeValueCheckKey);
            hadReadStandardCodes.add(codeValueCheckKey);
            if (rowIndex1 >= 0) {
                errorMsg = String.format("第%d行与前第%d行的[代码值（唯一）]重复",
                        currentRowIndex, rowIndex1 + 2);
                errorMsgs.add(errorMsg);
            }
            if (dbStandardDics.containsKey(obj.getBaseCode())) {
                errorMsg = String.format("第%d行的[代码名称]，系统中已存在", currentRowIndex);
                errorMsgs.add(errorMsg);
            }
            if (!errorMsgs.isEmpty()) {
                return new ExcelVerifyHandlerResult(false, String.join("；", errorMsgs));
            }
        }
        return new ExcelVerifyHandlerResult(true);
    }

    @Override
    public void close() {
        dbStandardDics.clear();
        hadReadStandardNames.clear();
        hadReadStandardCodes.clear();
    }

    /**
     * DapStandardBasicDic::getBaseCode, DapStandardBasicDic::getBaseCodeName
     *
     * @param dbStandardDics
     * @return
     */
    public BsAndDicVerifyHandler setdbStandardDics(Map<String, String> dbStandardDics) {
        this.dbStandardDics = dbStandardDics;
        return this;
    }

    @Override
    public Map<String, Object> getSheetCacheMap() {
        return null;
    }
}
